package com.example.config;

import com.example.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Configuration
@Slf4j
public class TestConfig implements ApplicationRunner {

    static final Map<Integer, Map<String, String>> map=new HashMap();
    static Integer j=0;

    static {
        for (int i = 0; i < 5; i++) {
            Map<String, String> mapTemp = new HashMap();
            mapTemp.put("name","lwd");
            mapTemp.put("age",i+"");
            map.put(i,mapTemp);
        }
    }

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    CommonUtils commonUtils;

    @Override
    public void run(ApplicationArguments args) throws Exception {

//        method01();

        String url="https://api.apiopen.top/getAllUrl";
        while (true){
            Thread.sleep(9000);
            String radom = getRadom(100);
            log.info("radom:{}",radom);
            Thread.sleep(9000);
            List forObject = restTemplate.getForObject(url, List.class);
        }
    }

    private String getRadom(int i) {
        Random random=new Random();
        int i1 = random.nextInt(i);
        try {
            int anInt = commonUtils.getInt(i1);
            log.info("anInt:{}",anInt);
        }catch (Exception e){
            log.info("err++++++++++++");
        }
        return String.valueOf(i1);
    }

    private void method01() {
        for (int i = 0; i < 5; i++) {
            Thread thread=new Thread(()->{
                Map map1=new HashMap();
                map1.put("name","lwd");
                map1.put("age",Math.random());
                restTemplate.postForObject("http://localhost:8081/v1/user/save_or_update", map1, Map.class);
            });
            thread.start();
        }
    }

}
